﻿using System;
using System.Windows;
using XmlStudio.ViewModel.Node;
using System.Windows.Data;
using System.Windows.Controls;

namespace XmlStudio.Dialogs {
    /// <summary>
    /// Interaction logic for AddAttributeDialog.xaml
    /// </summary>
    // ReSharper disable RedundantExtendsListEntry
    public partial class AddAttributeDialog : Window {
        // ReSharper restore RedundantExtendsListEntry
        #region Properties
        /// <summary>
        /// Gets the <see cref="XmlAttributeViewModel"/> as a new attribute.
        /// </summary>
        public XmlAttributeViewModel Attribute {
            get;
            private set;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="AddAttributeDialog"/> class.
        /// </summary>
        public AddAttributeDialog() {
            InitializeComponent();
        }
        #endregion

        #region Event handlers
        protected override void OnInitialized(EventArgs e) {
            base.OnInitialized(e);

            prefixTextBox.Focus();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            bool isValidationError = false;

            BindingExpression prefixExpression = prefixTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression localNameExpression = localNameTextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression namespaceURIExpression = namespaceURITextBox.GetBindingExpression(TextBox.TextProperty);
            BindingExpression valueExpression = valueTextBox.GetBindingExpression(TextBox.TextProperty);

            if(prefixExpression != null) {
                prefixExpression.ValidateWithoutUpdate();
                if(prefixExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(localNameExpression != null) {
                localNameExpression.ValidateWithoutUpdate();
                if(localNameExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(namespaceURIExpression != null) {
                namespaceURIExpression.ValidateWithoutUpdate();
                if(namespaceURIExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(valueExpression != null) {
                valueExpression.ValidateWithoutUpdate();
                if(valueExpression.HasError) {
                    isValidationError = true;
                }
            }

            if(isValidationError) {
                return;
            } else {
                prefixExpression.UpdateSource();
                localNameExpression.UpdateSource();
                namespaceURIExpression.UpdateSource();
                valueExpression.UpdateSource();

                this.Attribute = new XmlAttributeViewModel {
                    LocalName = localNameTextBox.Text,
                    Prefix = prefixTextBox.Text,
                    NamespaceURI = namespaceURITextBox.Text,
                    Value = valueTextBox.Text
                };

                this.DialogResult = true;
                this.Close();
            }
        }
        #endregion
    }
}
